<?php
require_once(Config::acquire('Model', null, 'model'));
require_once(Config::acquire('Probleem', MODEL));
require_once(Config::acquire('Incident', MODEL));
/**
 * Created by JetBrains PhpStorm.
 * User: Sander
 * Date: 30-6-11
 * Time: 7:32
 * To change this template use File | Settings | File Templates.
 */
 
class ProbleemIncident extends Model {

     /**
     * initialiseren variabelen
     */
    private $probleem;
    private $incident = array();

    /**
     * uitlezen van de gegevens
     */
    public static function read($id)
    {
        $incident = array();
        $list = SQL::query("SELECT * FROM `probleem_node` WHERE `Probleem_ID` = $id");
        while ($list->isValid()) {
            $incident[] = Incident::read($list->fetchField('Incident_ID'));
            $list->next();
        }
        return ProbleemIncident::create(Probleem::read($id), $incident);
    }

     /**
     * aanmaken van de variabelen
     */
    public function __construct(Probleem $probleem, $incident )
    {
        $this->probleem = $probleem;
        $this->incident = $incident;
    }

    /**
     * aanmaken van een nieuw item
     */
    public static function create(Probleem $probleem, $incident)
    {

        return new ProbleemIncident($probleem, $incident);
    }

     /**
     * Query voor deleten van de gegevens uit de gegeven tabel
     */
    public function delete($incidentID = null)
    {
        if (!is_null($incidentID)) {
            SQL::execute("DELETE FROM `probleem_node` WHERE `Probleem_ID` = '" . $this->probleem->getId() . "' AND `Incident_ID` = '$incidentID'");
        }
    }

    /**
     * Nieuwe toevoegen
     */
    public function add($incidentID)
    {
        SQL::execute("INSERT INTO `probleem_node` (
                        `Probleem_ID`,
                        `Incident_ID`)
                        VALUES ({$this->probleem->getID()},
                        {$incidentID})");
    }


    /*
     * schrijf naar array
     */
    public function toArray()
    {

    }

    /**
     * Query voor het updaten van de betreffende tabel
     */
    public function update()
    {
        return;
    }

    /*
     * unieke exemplaren uitzoeken
     */
    public function getUnique()
    {
        $list = array();
        foreach (Incident::readAll() as $incident) {
            $list[$incident->getId()] = $incident->getOmschrijving();
        }
        foreach ($this->incident as $incident)
            unset($list[$incident->getID()]);
        return $list;
    }

     /**
     * Setters & getters
     */
    public function setIncident($incident)
    {
        $this->incident = $incident;
    }

    public function getIncident()
    {
        return $this->incident;
    }

    public function setProbleem($probleem)
    {
        $this->probleem = $probleem;
    }

    public function getProbleem()
    {
        return $this->probleem;
    }

}
